package com.hmall.search.listener;


import com.hmall.common.constants.MqConstants;;
import com.hmall.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ItemListener {
    @Autowired
    private SearchService searchService;

    /**
     * 商品上架队列监听（声明交换机/队列/绑定关系）
     * @param
     */

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.ITEM_INSERT_QUEUE),
            exchange = @Exchange(name = MqConstants.ITEM_EXCHANGE,type = ExchangeTypes.DIRECT),
            key = MqConstants.ITEM_INSERT_KEY
    ))
    public void consumerInsertMessage(Long id) {


            System.out.println("上架接收到消息消费:" +id );
        searchService.insertById(id);}



    /**
     * 商品上架队列监听（声明交换机/队列/绑定关系）
     * @param
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.ITEM_DELETE_QUEUE),
            exchange = @Exchange(name = MqConstants.ITEM_EXCHANGE,type = ExchangeTypes.DIRECT),
            key = MqConstants.ITEM_DELETE_KEY
    ))
    public void consumerDeleteMessage(Long id) {
        System.out.println("下架接收到消息消费:" + id);
        searchService.deleteById(id);
    }

}
